package com.xilihui.xlh.business.activitys.store;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;

import com.facebook.drawee.view.SimpleDraweeView;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.xilihui.xlh.R;
import com.xilihui.xlh.business.entities.StoreDetailsEntity;
import com.xilihui.xlh.business.entities.StoreGoodDetailsEntity;
import com.xilihui.xlh.business.entities.TakeCodeEntity;
import com.xilihui.xlh.business.requests.MyAppointmentRequest;
import com.xilihui.xlh.business.requests.StoreRequest;
import com.xilihui.xlh.component.recyclerview.BaseAdapter;
import com.xilihui.xlh.component.recyclerview.BaseViewHolder;
import com.xilihui.xlh.constant.StrConst;
import com.xilihui.xlh.constant.UrlConst;
import com.xilihui.xlh.core.app.BaseCompatActivity;
import com.xilihui.xlh.core.http.DoTransform;
import com.xilihui.xlh.core.http.HttpSubscriber;
import com.xilihui.xlh.core.util.SPUtil;
import com.xilihui.xlh.core.util.ToastUtil;
import com.xilihui.xlh.core.util.YEventBuses;
import com.xilihui.xlh.core.util.YSPUtils;

import java.util.ArrayList;

import butterknife.BindView;

/**
 * Created by Administrator on 2018/9/28.
 */

public class QRcodeActivity extends BaseCompatActivity{

    @BindView(R.id.recyclerview)
    XRecyclerView recyclerView;

    String id = "";
    

    BaseAdapter<StoreGoodDetailsEntity.OrderInfoBean> beanBaseAdapter;
    ArrayList<StoreGoodDetailsEntity.OrderInfoBean> datas = new ArrayList<>();
    
    @Override
    public int getLayout() {
        return R.layout.activity_qrcode;
    }

    @Override
    public String initTitle() {
        return "领货码";
    }

    @Override
    public void initUI(@Nullable Bundle savedInstanceState) {

        id = getIntent().getStringExtra("order_id");

        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        View view = LayoutInflater.from(this).inflate(R.layout.recycler_take_good_code_head,null);
        SimpleDraweeView sdv = (SimpleDraweeView)view.findViewById(R.id.sdv_code_img);
        String access_token = (String) SPUtil.get(this,SPUtil.TOKEN,"");
        sdv.setImageURI(UrlConst.baseUrl()+"/api/order/get_goods_code?access_token="+access_token+"&id="+id);
        recyclerView.addHeaderView(view);
        recyclerView.setPullRefreshEnabled(false);

        beanBaseAdapter = new BaseAdapter<StoreGoodDetailsEntity.OrderInfoBean>(this,datas) {
            @Override
            protected int setLayoutId() {
                return R.layout.recycler_item_store_take_good;
            }

            @Override
            protected void getView(BaseViewHolder holder, StoreGoodDetailsEntity.OrderInfoBean entity, int position) {
                SimpleDraweeView simpleDraweeView = (SimpleDraweeView)holder.getView(R.id.iv_img);
                simpleDraweeView.setImageURI(UrlConst.baseUrl()+entity.getOrder().get(0).getOrder_goods().getOriginal_img());

                holder.setText(R.id.tv_name,entity.getOrder().get(0).getOrder_goods().getGoods_name());
                holder.setText(R.id.tv_model,entity.getOrder().get(0).getOrder_goods().getSpec_key_name());
                holder.setText(R.id.tv_num,"x"+entity.getOrder().get(0).getOrder_goods().getGoods_num());
                holder.setText(R.id.tv_price,"￥"+entity.getOrder().get(0).getGoods_price());
                
                int num = Integer.parseInt(entity.getOrder().get(0).getOrder_goods().getGoods_num());
                float price = Float.valueOf(entity.getOrder().get(0).getGoods_price());
                float totalPrice = num * price;
                
                holder.setText(R.id.tv_totalNum,"共"+entity.getOrder().get(0).getOrder_goods().getGoods_num()+
                "件商品 合计：");
                holder.setText(R.id.tv_totalPrice,"￥"+totalPrice);
                
                
            }

        };


        recyclerView.setAdapter(beanBaseAdapter);
        beanBaseAdapter.setAnimationsLocked(true);
        
    }

    @Override
    public void initData() {
        getData();
    }

    public void getData(){
        StoreRequest.orderAllInfo(QRcodeActivity.this,id,"")
                .compose(DoTransform.<StoreGoodDetailsEntity>applyScheduler(QRcodeActivity.this,true))
                .compose(this.<StoreGoodDetailsEntity>bindToLifecycle())
                .subscribe(new HttpSubscriber<StoreGoodDetailsEntity>(this) {
                    @Override
                    protected void onSuccess(StoreGoodDetailsEntity entity) {
                        datas.clear();
                        datas.add(entity.getOrder_info());
                        beanBaseAdapter.setList(datas);
                    }

                    @Override
                    protected void onFail(String msg) {
                        ToastUtil.toastShortNegative(msg);
                    }
                });
    }
    
    @Override
    public void onEventMainThread(YEventBuses.Event event) {
        super.onEventMainThread(event);
        if(event.is(StrConst.UMENG_PUSH)){
            if(event.getParams(0).equals("8")){
                YEventBuses.post(new YEventBuses.Event("yisouhuo"));
                finish();
            }
        }
    }
    
    
}
